import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class CollisionBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CollisionBox extends Actor
{
    private int posX;
    private int posY;
    private int type;
    private Actor actor;
    
    
    public CollisionBox(Actor act,int x,int y,int width,int height)
    {
        /*GreenfootImage image=new GreenfootImage(width,height);
        image.setColor(new Color(0,0,0,100));
        image.fill();
        setImage(image);*/
        
        /*image.setColor(new Color(0,0,0,0));
        image.fill();
        setImage(image);*/
        
        /*posX=x;
        posY=y;*/
        actor=act;
        updateImage(x,y,width,height);
    }
        
        
    public void act() 
    {
        if(actor.getWorld()==null)
        {
            getWorld().removeObject(this);
            return;
        }else
        setLocation(actor.getX()+posX,actor.getY()+posY);  
        
        
        GreenfootImage image=getImage();
        
        if(((GameWorld)getWorld()).isDebugOn())
        {
            image.setTransparency(255);
            setImage(image);
        }else
        {
            image.setTransparency(5);
            setImage(image);
        }
        
    }    
    
    public boolean isTouchingGround()
    {
            if(isIntersecting(Ground.class))        
            return(true);
            else
            return(false);
    }
    
    public boolean isTouchingWall()
    {
        if(isIntersecting(Wall.class))
        return(true);
        else
        return(false);
    }
    
    public boolean isTouchingWhip()
    {          
        if(isIntersecting(Whip.class))
        return(true);
        else
        return(false);
    }
    
    public void updateHitbox(int action,int dir)
    {
        switch(action)
        {
            case 0: 
            case 1:
            case 2:
            case 5:
            case 6:     updateImage(-3,0,34,90);
                        break;
            case 3:     updateImage(0,20,34,60);
                        break;
            case 4:     updateImage(0,27,34,34);
                        break;
            case 7:
            case 8:     updateImage(0,27,50,34);
                        break;
        }   
        
        if(dir==1)
        posX=-posX;
    }
    
    public void updateForAction(int action,int dir)
    {
        switch(action)
        {
            case 0: 
            case 1:
            case 2:
            case 5:
            case 6:     updateImage(40,0,10,10);
                        break;
            case 3:     updateImage(40,20,10,10);
                        break;
            case 4:     updateImage(50,27,10,10);
                        break;
            case 7:
            case 8:     updateImage(40,27,10,10);
                        break;
        }   
        
        if(dir==1)
        posX=-posX;
    }
    
    public void updateImage(int x,int y,int width,int height)
    {
        GreenfootImage image=new GreenfootImage(width,height);
        image.setColor(new Color(225,0,255,60));
        image.fill();
        image.setTransparency(5);
        setImage(image);
        
        posX=x;
        posY=y;
    }
    
    private boolean isIntersecting(java.lang.Class cls)
    {
        java.util.List actors=getIntersectingObjects(cls);
        
        if(actors.isEmpty())
        return(false);
        
        if(isVisuallyIntersecting((Actor)actors.get(0)))
        return(true);
        else
            if(actors.size()>1)
            return(isVisuallyIntersecting((Actor)actors.get(1)));
            
       return(false);
    }
    
    private boolean isVisuallyIntersecting(Actor actor)    
    {    
        // get width and height of images    
        int wA = getImage().getWidth();    
        int hA = getImage().getHeight();    
        int wB = actor.getImage().getWidth();    
        int hB = actor.getImage().getHeight();    
        // get world coordinates of origin point (0, 0) of images    
        int xAw = getX()-wA/2;    
        int yAw = getY()-hA/2;    
        int xBw = actor.getX()-wB/2;    
        int yBw = actor.getY()-hB/2;    
        // determine (x, y) on each image to begin checking from    
        // and dimensions of area to check    
        int xA = 0, yA = 0, xB = 0, yB = 0; // initialize start coord fields    
        int w = 0, h = 0; // initialize scan dimension fields    
        // determine values for fields just initialized    
        if (xAw > xBw)    
        {    
            xB = xAw-xBw;    
            if (xAw+wA > xBw+wB) w = wB-xB; else w = wA;    
        }    
        else    
        {    
            xA = xBw-xAw;    
            if (xBw+wB > xAw+wA) w = wA-xA; else w = wB;    
        }    
        if (yAw > yBw)    
        {    
            yB = yAw-yBw;    
            if (yAw+hA > yBw+hB) h = hB-yB; else h = hA;    
        }    
        else    
        {    
            yA = yBw-yAw;    
            if (yBw+hB > yAw+hA) h = hA-yA; else h = hB;    
        }    
        // perform checking    
        java.awt.Color trans = new java.awt.Color(0, 0, 0, 0);    
        for (int x=0; x<w; x++) for (int y=0; y<h; y++)    
            {    
                if (trans.equals(getImage().getColorAt(xA+x, yA+y)) || trans.equals(actor.getImage().getColorAt(xB+x, yB+y)))   
                    return false;
        }    
        return true;    
    } 
}
